<?php  
class ControllerSaleInvitation extends Controller {
	private $error = array();

	public function index() {
		$this->language->load('sale/invitation');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('sale/invitation');

		$this->getList();
	}

	public function insert() {
		$this->language->load('sale/invitation');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('sale/invitation');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            for($i=1;$i<=$this->request->post['amount'];$i++){
                $this->model_sale_invitation->addInvitation($this->request->post);
            }

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('sale/invitation', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}

	public function delete() {
		$this->language->load('sale/invitation');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('sale/invitation');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $invitation_id) {
				$this->model_sale_invitation->deleteInvitation($invitation_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->redirect($this->url->link('sale/invitation', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getList();
	}


    public function subscribe(){
        $this->load->model('sale/invitation');
        if (isset($this->request->get['invitation_id'])) {
            $this->model_sale_invitation->SetInvitationSubscribe($this->request->get['invitation_id']);
        }
        $this->session->data['success'] = $this->language->get('text_success');
        $this->redirect($this->url->link('sale/invitation', 'token=' . $this->session->data['token'],'SSL'));
    }
	protected function getList() {
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'name';
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('sale/invitation', 'token=' . $this->session->data['token'] . $url, 'SSL'),
			'separator' => ' :: '
		);

		$this->data['insert'] = $this->url->link('sale/invitation/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$this->data['delete'] = $this->url->link('sale/invitation/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');

		$this->data['invitations'] = array();

		$data = array(
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);

		$invitation_total = $this->model_sale_invitation->getTotalInvitations();

		$results = $this->model_sale_invitation->getInvitations($data);

		foreach ($results as $result) {
			$action = array();

			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('sale/invitation/update', 'token=' . $this->session->data['token'] . '&invitation_id=' . $result['invitation_id'] . $url, 'SSL')
			);


            if($result['status']==0){
                $invitation_status="未使用";
            }elseif($result['status']==1){
                $invitation_status="已使用";
            }elseif($result['status']==2){
                $invitation_status="已预订";
            }




			$this->data['invitations'][] = array(
				'invitation_id'  => $result['invitation_id'],
				'code'       => $result['code'],
				'date_activation' => $result['date_activation'],
				'date_added'   => $result['date_added'],
                'selected'   => isset($this->request->post['selected']) && in_array($result['coupon_id'], $this->request->post['selected']),
                'firstname'   => $result['firstname'],
                'lastname'   => $result['lastname'],
                'customer_id'   => $result['customer_id'],
                'customer_href'  =>$this->url->link('sale/customer/update', 'token=' . $this->session->data['token'] . '&customer_id=' . $result['customer_id'] . $url, 'SSL'),
                'status' =>$invitation_status,
                'subscribe'  =>$this->url->link('sale/invitation/subscribe', 'token=' . $this->session->data['token'] . '&invitation_id=' . $result['invitation_id'] . $url, 'SSL')
			);
		}

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_code'] = $this->language->get('column_code');
		$this->data['column_discount'] = $this->language->get('column_discount');
		$this->data['column_date_start'] = $this->language->get('column_date_start');
		$this->data['column_date_end'] = $this->language->get('column_date_end');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_action'] = $this->language->get('column_action');		

		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$this->data['sort_name'] = HTTPS_SERVER . 'index.php?route=sale/invitation&token=' . $this->session->data['token'] . '&sort=name' . $url;
		$this->data['sort_code'] = HTTPS_SERVER . 'index.php?route=sale/invitation&token=' . $this->session->data['token'] . '&sort=code' . $url;
		$this->data['sort_discount'] = HTTPS_SERVER . 'index.php?route=sale/invitation&token=' . $this->session->data['token'] . '&sort=discount' . $url;
		$this->data['sort_date_start'] = HTTPS_SERVER . 'index.php?route=sale/invitation&token=' . $this->session->data['token'] . '&sort=date_start' . $url;
		$this->data['sort_date_end'] = HTTPS_SERVER . 'index.php?route=sale/invitation&token=' . $this->session->data['token'] . '&sort=date_end' . $url;
		$this->data['sort_status'] = HTTPS_SERVER . 'index.php?route=sale/invitation&token=' . $this->session->data['token'] . '&sort=status' . $url;
		$this->data['sort_customer_id'] = HTTPS_SERVER . 'index.php?route=sale/invitation&token=' . $this->session->data['token'] . '&sort=customer_id' . $url;
		$this->data['sort_date_activation'] = HTTPS_SERVER . 'index.php?route=sale/invitation&token=' . $this->session->data['token'] . '&sort=date_activation' . $url;
		$this->data['sort_date_added'] = HTTPS_SERVER . 'index.php?route=sale/invitation&token=' . $this->session->data['token'] . '&sort=date_added' . $url;

		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $invitation_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = HTTPS_SERVER . 'index.php?route=sale/invitation&token=' . $this->session->data['token'] . $url . '&page={page}';

		$this->data['pagination'] = $pagination->render();

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'sale/invitation_list.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);

		$this->response->setOutput($this->render());
	}

	protected function getForm() {
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
		$this->data['text_percent'] = $this->language->get('text_percent');
		$this->data['text_amount'] = $this->language->get('text_amount');

		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_description'] = $this->language->get('entry_description');
		$this->data['entry_code'] = $this->language->get('entry_code');
		$this->data['entry_discount'] = $this->language->get('entry_discount');
		$this->data['entry_logged'] = $this->language->get('entry_logged');
		$this->data['entry_shipping'] = $this->language->get('entry_shipping');
		$this->data['entry_type'] = $this->language->get('entry_type');
		$this->data['entry_total'] = $this->language->get('entry_total');
		$this->data['entry_category'] = $this->language->get('entry_category');
		$this->data['entry_product'] = $this->language->get('entry_product');
		$this->data['entry_date_start'] = $this->language->get('entry_date_start');
		$this->data['entry_date_end'] = $this->language->get('entry_date_end');
		$this->data['entry_uses_total'] = $this->language->get('entry_uses_total');
		$this->data['entry_uses_customer'] = $this->language->get('entry_uses_customer');
		$this->data['entry_status'] = $this->language->get('entry_status');

		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

		$this->data['tab_general'] = $this->language->get('tab_general');
		$this->data['tab_history'] = $this->language->get('tab_history');

		$this->data['token'] = $this->session->data['token'];

		if (isset($this->request->get['invitation_id'])) {
			$this->data['invitation_id'] = $this->request->get['invitation_id'];
		} else {
			$this->data['invitation_id'] = 0;
		}

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = '';
		}

		if (isset($this->error['code'])) {
			$this->data['error_code'] = $this->error['code'];
		} else {
			$this->data['error_code'] = '';
		}		

		if (isset($this->error['date_start'])) {
			$this->data['error_date_start'] = $this->error['date_start'];
		} else {
			$this->data['error_date_start'] = '';
		}	

		if (isset($this->error['date_end'])) {
			$this->data['error_date_end'] = $this->error['date_end'];
		} else {
			$this->data['error_date_end'] = '';
		}	

		$url = '';

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('sale/invitation', 'token=' . $this->session->data['token'] . $url, 'SSL'),
			'separator' => ' :: '
		);

		if (!isset($this->request->get['invitation_id'])) {
			$this->data['action'] = $this->url->link('sale/invitation/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
		} else {
			$this->data['action'] = $this->url->link('sale/invitation/update', 'token=' . $this->session->data['token'] . '&invitation_id=' . $this->request->get['invitation_id'] . $url, 'SSL');
		}

		$this->data['cancel'] = $this->url->link('sale/invitation', 'token=' . $this->session->data['token'] . $url, 'SSL');

		if (isset($this->request->get['invitation_id']) && (!$this->request->server['REQUEST_METHOD'] != 'POST')) {
			$invitation_info = $this->model_sale_invitation->getInvitation($this->request->get['invitation_id']);
		}

		if (isset($this->request->post['name'])) {
			$this->data['name'] = $this->request->post['name'];
		} elseif (!empty($invitation_info)) {
			$this->data['name'] = $invitation_info['name'];
		} else {
			$this->data['name'] = '';
		}

		if (isset($this->request->post['code'])) {
			$this->data['code'] = $this->request->post['code'];
		} elseif (!empty($invitation_info)) {
			$this->data['code'] = $invitation_info['code'];
		} else {
			$this->data['code'] = '';
		}

		if (isset($this->request->post['type'])) {
			$this->data['type'] = $this->request->post['type'];
		} elseif (!empty($invitation_info)) {
			$this->data['type'] = $invitation_info['type'];
		} else {
			$this->data['type'] = '';
		}

		if (isset($this->request->post['discount'])) {
			$this->data['discount'] = $this->request->post['discount'];
		} elseif (!empty($invitation_info)) {
			$this->data['discount'] = $invitation_info['discount'];
		} else {
			$this->data['discount'] = '';
		}

		if (isset($this->request->post['logged'])) {
			$this->data['logged'] = $this->request->post['logged'];
		} elseif (!empty($invitation_info)) {
			$this->data['logged'] = $invitation_info['logged'];
		} else {
			$this->data['logged'] = '';
		}

		if (isset($this->request->post['shipping'])) {
			$this->data['shipping'] = $this->request->post['shipping'];
		} elseif (!empty($invitation_info)) {
			$this->data['shipping'] = $invitation_info['shipping'];
		} else {
			$this->data['shipping'] = '';
		}

		if (isset($this->request->post['total'])) {
			$this->data['total'] = $this->request->post['total'];
		} elseif (!empty($invitation_info)) {
			$this->data['total'] = $invitation_info['total'];
		} else {
			$this->data['total'] = '';
		}

		if (isset($this->request->post['invitation_product'])) {
			$products = $this->request->post['invitation_product'];
		} elseif (isset($this->request->get['invitation_id'])) {		
			$products = $this->model_sale_invitation->getInvitationProducts($this->request->get['invitation_id']);
		} else {
			$products = array();
		}

		$this->load->model('catalog/product');

		$this->data['invitation_product'] = array();

		foreach ($products as $product_id) {
			$product_info = $this->model_catalog_product->getProduct($product_id);

			if ($product_info) {
				$this->data['invitation_product'][] = array(
					'product_id' => $product_info['product_id'],
					'name'       => $product_info['name']
				);
			}
		}

		if (isset($this->request->post['invitation_category'])) {
			$categories = $this->request->post['invitation_category'];
		} elseif (isset($this->request->get['invitation_id'])) {		
			$categories = $this->model_sale_invitation->getInvitationCategories($this->request->get['invitation_id']);
		} else {
			$categories = array();
		}

		$this->load->model('catalog/category');

		$this->data['invitation_category'] = array();

		foreach ($categories as $category_id) {
			$category_info = $this->model_catalog_category->getCategory($category_id);

			if ($category_info) {
				$this->data['invitation_category'][] = array(
					'category_id' => $category_info['category_id'],
					'name'        => ($category_info['path'] ? $category_info['path'] . ' &gt; ' : '') . $category_info['name']
				);
			}
		}

		if (isset($this->request->post['date_start'])) {
			$this->data['date_start'] = $this->request->post['date_start'];
		} elseif (!empty($invitation_info)) {
			$this->data['date_start'] = date('Y-m-d', strtotime($invitation_info['date_start']));
		} else {
			$this->data['date_start'] = date('Y-m-d', time());
		}

		if (isset($this->request->post['date_end'])) {
			$this->data['date_end'] = $this->request->post['date_end'];
		} elseif (!empty($invitation_info)) {
			$this->data['date_end'] = date('Y-m-d', strtotime($invitation_info['date_end']));
		} else {
			$this->data['date_end'] = date('Y-m-d', time());
		}

		if (isset($this->request->post['uses_total'])) {
			$this->data['uses_total'] = $this->request->post['uses_total'];
		} elseif (!empty($invitation_info)) {
			$this->data['uses_total'] = $invitation_info['uses_total'];
		} else {
			$this->data['uses_total'] = 1;
		}

		if (isset($this->request->post['uses_customer'])) {
			$this->data['uses_customer'] = $this->request->post['uses_customer'];
		} elseif (!empty($invitation_info)) {
			$this->data['uses_customer'] = $invitation_info['uses_customer'];
		} else {
			$this->data['uses_customer'] = 1;
		}

		if (isset($this->request->post['status'])) {
			$this->data['status'] = $this->request->post['status'];
		} elseif (!empty($invitation_info)) {
			$this->data['status'] = $invitation_info['status'];
		} else {
			$this->data['status'] = 1;
		}

		$this->template = 'sale/invitation_form.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);

		$this->response->setOutput($this->render());		
	}

	protected function validateForm() {
		if (!$this->user->hasPermission('modify', 'sale/invitation')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

        if(!is_numeric($this->request->post['amount'])&&$this->request->post['amount']!='0'){
            $this->error['amount'] ="请填写大于0的数字";
        }

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	protected function validateDelete() {
		if (!$this->user->hasPermission('modify', 'sale/invitation')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

}
?>